package com.wanho.java159.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * @author zhang
 * @version 1.0
 * @date 2021/7/12
 */
@Component
public class OSSUtil {
    @Value("${oss.endpoint}")
    private String endPoint ;
    @Value("${oss.accessKeyId}")
    private String accessKeyId ;
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret ;
    @Value("${oss.bucketName}")
    private String bucketName ;
    @Value("${oss.objectName}")
    private String objectName ;
    @Value("${oss.imageStyle}")
    private String imageStyle ;
    @Value("${oos.imageLocationUrl}")
    private String imageLocationUrl ;

    /**
     * 图片上传
     * @param picName 图片名
     * @param inputStream 输入流
     * @return 外网访问的图片地址
     */
    public String putImage2OSS(String picName, InputStream inputStream){
        //创建 OSS实例
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        //上传文件到指定的存储空间
        ossClient.putObject(bucketName,objectName+picName,inputStream) ;
        //关闭OSSClient对象
        ossClient.shutdown();
        return imageLocationUrl+picName+imageStyle ;
    }
}
